
#import "KrVideoPlayerControlView.h"

static const CGFloat kVideoControlBarHeight = 40.0;
static const CGFloat kVideoControlAnimationTimeinterval = 0.3;
static const CGFloat kVideoControlTimeLabelFontSize = 10.0;
static const CGFloat kVideoControlBarAutoFadeOutTimeinterval = 5.0;

@interface KrVideoPlayerControlView()<UITableViewDelegate>

@property (nonatomic, strong) UIView *topBar;
@property (nonatomic, strong) UIView *bottomBar;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *pauseButton;
@property (nonatomic, strong) UIButton *fullScreenButton;
@property (nonatomic, strong) UIButton *shrinkScreenButton;
@property (nonatomic, strong) UISlider *progressSlider;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, assign) BOOL isBarShowing;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;


@end
@implementation KrVideoPlayerControlView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.leftview];
        [self addSubview:self.rightview];
        [self addSubview:self.topBar];
        [self.topBar addSubview:self.closeButton];
        [self.topBar addSubview:self.titleLabel];
        self.topBar.hidden = YES;
        [self addSubview:self.bottomBar];
        [self.bottomBar addSubview:self.playButton];
        [self.bottomBar addSubview:self.pauseButton];
        self.pauseButton.hidden = YES;
        [self.bottomBar addSubview:self.fullScreenButton];
        [self.bottomBar addSubview:self.shrinkScreenButton];
        self.shrinkScreenButton.hidden = YES;
        [self.bottomBar addSubview:self.progressSlider];
        [self.bottomBar addSubview:self.timeLabel];
        [self.bottomBar addSubview:self.timeAllLabel];
        [self addSubview:self.shengyiMove];
        self.shengyiMove.hidden = YES;
        [self addSubview:self.liangduMove];
        self.liangduMove.hidden = YES;
        [self addSubview:self.indicatorView];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
        [self addGestureRecognizer:tapGesture];
        
        [self.bottomBar addSubview:self.showDefinitionView];
        self.showDefinitionView.hidden = YES;
        
        [self addSubview:self.definitionView];
//        self.definitionView.hidden = NO;
        self.definitionView.alpha=0;
        
        [self.definitionView addSubview:self.biaoqingBtn];
        [self.definitionView addSubview:self.gaoqingBtn];
        
        [self.topBar addSubview:self.videoListBtn];
        

        
//        UISwipeGestureRecognizer *rightSwi = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiAction:)];
//        rightSwi.direction = UISwipeGestureRecognizerDirectionRight;
//        [self addGestureRecognizer:rightSwi];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.topBar.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds), CGRectGetWidth(self.bounds), kVideoControlBarHeight);
    //    self.closeButton.frame = CGRectMake(CGRectGetWidth(self.topBar.bounds) - CGRectGetWidth(self.closeButton.bounds), CGRectGetMinX(self.topBar.bounds), CGRectGetWidth(self.closeButton.bounds), CGRectGetHeight(self.closeButton.bounds));
    self.bottomBar.frame = CGRectMake(CGRectGetMinX(self.bounds), CGRectGetHeight(self.bounds) - kVideoControlBarHeight, CGRectGetWidth(self.bounds), kVideoControlBarHeight);
    self.playButton.frame = CGRectMake(CGRectGetMinX(self.bottomBar.bounds), CGRectGetHeight(self.bottomBar.bounds)/2 - CGRectGetHeight(self.playButton.bounds)/2, CGRectGetWidth(self.playButton.bounds), CGRectGetHeight(self.playButton.bounds));
    self.pauseButton.frame = self.playButton.frame;
    self.fullScreenButton.frame = CGRectMake(CGRectGetWidth(self.bottomBar.bounds) - CGRectGetWidth(self.fullScreenButton.bounds), CGRectGetHeight(self.bottomBar.bounds)/2 - CGRectGetHeight(self.fullScreenButton.bounds)/2, CGRectGetWidth(self.fullScreenButton.bounds), CGRectGetHeight(self.fullScreenButton.bounds));
    self.shrinkScreenButton.frame = self.fullScreenButton.frame;
//    self.progressSlider.frame = CGRectMake(CGRectGetMaxX(self.playButton.frame), CGRectGetHeight(self.bottomBar.bounds)/2 - CGRectGetHeight(self.progressSlider.bounds)/2, CGRectGetMinX(self.fullScreenButton.frame) - CGRectGetMaxX(self.playButton.frame), CGRectGetHeight(self.progressSlider.bounds));
    self.progressSlider.width = self.bottomBar.width / 2;
    self.progressSlider.left = (self.bottomBar.width - self.progressSlider.width) / 2;
    self.progressSlider.top = (self.bottomBar.height - self.progressSlider.height) / 2;
//    self.timeLabel.frame = CGRectMake(CGRectGetMidX(self.progressSlider.frame), CGRectGetHeight(self.bottomBar.bounds) - CGRectGetHeight(self.timeLabel.bounds) - 2.0, CGRectGetWidth(self.progressSlider.bounds)/2, CGRectGetHeight(self.timeLabel.bounds));
    self.timeLabel.width = 80;
    self.timeLabel.right = self.progressSlider.left - 5;
    self.timeLabel.top = (self.bottomBar.height - self.timeLabel.height) / 2;

    
    self.timeAllLabel.width = 80;
    self.timeAllLabel.left = self.progressSlider.right + 5;
    self.timeAllLabel.top = (self.bottomBar.height - self.timeAllLabel.height) / 2;
    
    self.showDefinitionView.right = self.shrinkScreenButton.left - 10;
    self.showDefinitionView.top = (self.bottomBar.height - self.showDefinitionView.height) / 2;
    
    self.definitionView.bottom = self.bottomBar.top;
    self.definitionView.left = self.showDefinitionView.left;
    self.definitionView.width = self.showDefinitionView.width;
    self.definitionView.height = 2 * kVideoControlBarHeight;
    
    self.biaoqingBtn.frame=CGRectMake(0, 0, self.definitionView.width, kVideoControlBarHeight);
    self.gaoqingBtn.frame=CGRectMake(0, self.biaoqingBtn.bottom, self.definitionView.width, kVideoControlBarHeight);
    
    self.videoListBtn.right=CGRectGetWidth(self.topBar.bounds)-100;
    self.videoListBtn.bottom=CGRectGetHeight(self.topBar.bounds)-5;
    

    
    self.closeButton.left = 5;
    self.closeButton.top = (self.topBar.height - self.closeButton.height) / 2;
    
    self.titleLabel.left = self.closeButton.right;
    self.titleLabel.top = (self.topBar.height - self.titleLabel.height) / 2;
    
    self.leftview.width = self.width / 2;
    self.leftview.height = self.height;
    self.leftview.left = 0;
    self.leftview.top = 0;
    
    self.rightview.width = self.width / 2;
    self.rightview.height = self.height;
    self.rightview.right = self.width;
    self.rightview.top = 0;
    
    //self.shengyiMove.height = self.height - 65;
    self.shengyiMove.left = 0;
    self.shengyiMove.top = (self.height - self.shengyiMove.height) / 2;
    
    //self.liangduMove.height = self.height - 65;
    self.liangduMove.right = self.width;
    self.liangduMove.top = (self.height - self.liangduMove.height) / 2;
    
    self.indicatorView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    
}

- (void)didMoveToSuperview//视图移动完成后调用
{
    [super didMoveToSuperview];
    self.isBarShowing = YES;
}

- (void)animateHide
{
    if (!self.isBarShowing) {
        return;
    }
    [UIView animateWithDuration:kVideoControlAnimationTimeinterval animations:^{
        self.topBar.alpha = 0.0;
        self.bottomBar.alpha = 0.0;
        self.shengyiMove.alpha = 0.0;
        self.liangduMove.alpha = 0.0;
        self.definitionView.alpha = 0.0;
        self.showDefinitionView.selected=NO;
    } completion:^(BOOL finished) {
        self.isBarShowing = NO;
    }];
}

- (void)animateShow
{
    if (self.isBarShowing) {
        return;
    }
    [UIView animateWithDuration:kVideoControlAnimationTimeinterval animations:^{
        self.topBar.alpha = 1.0;
        self.bottomBar.alpha = 1.0;
        self.shengyiMove.alpha = 1.0;
        self.liangduMove.alpha = 1.0;
    } completion:^(BOOL finished) {
        self.isBarShowing = YES;
        [self autoFadeOutControlBar];
    }];
}

- (void)autoFadeOutControlBar
{
    if (!self.isBarShowing) {
        return;
    }
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateHide) object:nil];
    [self performSelector:@selector(animateHide) withObject:nil afterDelay:kVideoControlBarAutoFadeOutTimeinterval];
}

- (void)cancelAutoFadeOutControlBar
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateHide) object:nil];
}

- (void)onTap:(UITapGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateRecognized) {
        if (self.isBarShowing) {
            [self animateHide];
        } else {
            [self animateShow];
        }
    }
}

- (void)swiAction:(UISwipeGestureRecognizer *)swipe
{
    if (swipe.state == UIGestureRecognizerStateRecognized)
    {
        self.progressSlider.value += 0.02;
    }
    
}

#pragma mark - Property

- (UIView *)topBar
{
    if (!_topBar) {
        _topBar = [UIView new];
        _topBar.backgroundColor = RGBCOLOR(0, 0, 0, .5);
    }
    return _topBar;
}

- (UIView *)bottomBar
{
    if (!_bottomBar) {
        _bottomBar = [UIView new];
        _bottomBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    }
    return _bottomBar;
}

- (UIView *)definitionView
{
    if (!_definitionView) {
        _definitionView = [UIView new];
        _definitionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    }
    return _definitionView;
}

- (UIButton *)playButton
{
    if (!_playButton) {
        _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playButton setImage:[UIImage imageNamed:@"video_2"] forState:UIControlStateNormal];
        _playButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
    }
    return _playButton;
}

- (UIButton *)pauseButton
{
    if (!_pauseButton) {
        _pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_pauseButton setImage:[UIImage imageNamed:@"video_3"] forState:UIControlStateNormal];
        _pauseButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
    }
    return _pauseButton;
}

- (UIButton *)fullScreenButton
{
    if (!_fullScreenButton) {
        _fullScreenButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fullScreenButton setImage:[UIImage imageNamed:@"video_7"] forState:UIControlStateNormal];
        _fullScreenButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
    }
    return _fullScreenButton;
}

- (UIButton *)shrinkScreenButton
{
    if (!_shrinkScreenButton) {
        _shrinkScreenButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shrinkScreenButton setImage:[UIImage imageNamed:@"video_7"] forState:UIControlStateNormal];
        _shrinkScreenButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
    }
    return _shrinkScreenButton;
}

- (UISlider *)progressSlider
{
    if (!_progressSlider) {
        _progressSlider = [[UISlider alloc] init];
        [_progressSlider setThumbImage:[UIImage imageNamed:@"video_6"] forState:UIControlStateNormal];
        [_progressSlider setMinimumTrackTintColor:RGBCOLOR(57, 144, 206, 1)];
        [_progressSlider setMaximumTrackTintColor:RGBCOLOR(131, 131, 131, 131)];
        _progressSlider.value = 0.f;
        _progressSlider.continuous = YES;
    }
    return _progressSlider;
}

- (UIButton *)closeButton
{
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"top_9"] forState:UIControlStateNormal];
        _closeButton.bounds = CGRectMake(0, 0, kVideoControlBarHeight, kVideoControlBarHeight);
    }
    return _closeButton;
}

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.font = [UIFont systemFontOfSize:kVideoControlTimeLabelFontSize];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.bounds = CGRectMake(0, 0, kVideoControlTimeLabelFontSize, kVideoControlTimeLabelFontSize);
    }
    return _timeLabel;
}

- (UILabel *)timeAllLabel
{
    if (!_timeAllLabel)
    {
        _timeAllLabel = [UILabel new];
        _timeAllLabel.backgroundColor = [UIColor clearColor];
        _timeAllLabel.font = [UIFont systemFontOfSize:kVideoControlTimeLabelFontSize];
        _timeAllLabel.textColor = [UIColor whiteColor];
        _timeAllLabel.bounds = CGRectMake(0, 0, kVideoControlTimeLabelFontSize, kVideoControlTimeLabelFontSize);
    }
    return _timeAllLabel;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [UILabel new];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont fontWithName:FontNameYaHei size:13];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.bounds = CGRectMake(0, 0, 200, 20);
    }
    return _titleLabel;
}

//初始化
- (UIActivityIndicatorView *)indicatorView
{
    if (!_indicatorView) {
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        [_indicatorView stopAnimating];//停止
    }
    return _indicatorView;
}

- (UIView *)leftview
{
    if (!_leftview)
    {
        _leftview = [[UIView alloc] init];;
        _leftview.backgroundColor = [UIColor clearColor];
        
        //平移手势
        UIPanGestureRecognizer *leftPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(leftPanAction:)];
        [_leftview addGestureRecognizer:leftPan];
        _leftview.userInteractionEnabled = YES;
        
//        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(shengyiSwipe:)];
//        swipe.direction = UISwipeGestureRecognizerDirectionUp;
//        _leftview.userInteractionEnabled = YES;
//        [_leftview addGestureRecognizer:swipe];
    }
    return _leftview;
}

- (UIView *)rightview
{
    if (!_rightview)
    {
        _rightview = [[UIView alloc] init];;
        _rightview.backgroundColor = [UIColor clearColor];
        
        UIPanGestureRecognizer *rightPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rightPanAction:)];
        [_rightview addGestureRecognizer:rightPan];
        _rightview.userInteractionEnabled = YES;
    }
    return _rightview;
}

- (DCMoveVolu *)shengyiMove
{
    if (!_shengyiMove)
    {
        _shengyiMove = [[DCMoveVolu alloc] initWithType:1];
    }
    
    return _shengyiMove;
}

- (DCMoveVolu *)liangduMove
{
    if (!_liangduMove)
    {
        _liangduMove = [[DCMoveVolu alloc] initWithType:2];
    }
    
    return _liangduMove;
}

- (UIButton *)showDefinitionView
{
    if (!_showDefinitionView)
    {
        _showDefinitionView = [UIButton buttonWithType:UIButtonTypeCustom];
        _showDefinitionView.size = CGSizeMake(50, 40);
        _showDefinitionView.titleLabel.font = [UIFont fontWithName:FontNameYaHei size:14];
        [_showDefinitionView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_showDefinitionView setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [_showDefinitionView setTitle:@"标清" forState:UIControlStateNormal];
    }
    return _showDefinitionView;
}

- (UIButton *)biaoqingBtn
{
    if (!_biaoqingBtn) {
        _biaoqingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _biaoqingBtn.titleLabel.font = [UIFont fontWithName:FontNameYaHei size:14];
        [_biaoqingBtn setTitle:@"标清" forState:UIControlStateNormal];
        [_biaoqingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_biaoqingBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    }
    return _biaoqingBtn;
}

-(UIButton *)gaoqingBtn
{
    if (!_gaoqingBtn) {
        _gaoqingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _gaoqingBtn.titleLabel.font = [UIFont fontWithName:FontNameYaHei size:14];
        [_gaoqingBtn setTitle:@"高清" forState:UIControlStateNormal];
        [_gaoqingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_gaoqingBtn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    }
    return _gaoqingBtn;
}

-(UIButton *)videoListBtn
{
    if (!_videoListBtn) {
        _videoListBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _videoListBtn.size=CGSizeMake(80, 30);
        _videoListBtn.titleLabel.font = [UIFont fontWithName:FontNameYaHei size:14];
        [_videoListBtn setTitle:@"视频列表" forState:UIControlStateNormal];
        [_videoListBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _videoListBtn;
}




#pragma mark 触发事件
- (void)shengyiSwipe:(UISwipeGestureRecognizer *)swipe
{
    [MPMusicPlayerController applicationMusicPlayer].volume += 0.1;
}

- (void)leftPanAction:(UIPanGestureRecognizer *)pan
{
    CGPoint old = [pan velocityInView:pan.view];
    CGPoint point = [pan translationInView:pan.view];
    CGFloat volume = [MPMusicPlayerController applicationMusicPlayer].volume;
    
    NSLog(@"x==%@ , y==%@",String(old.x),String(old.y));
    
    if (ABS(ABS(point.x) - old.x) < 10)
    {
        if (point.y < 0)
        {
            if (volume < 1.0)
            {
                [[MPMusicPlayerController applicationMusicPlayer] setVolume: volume + ABS(point.y) / 1000.0];
            }
            
        }
        else
        {
            if (volume > 0)
            {
                [[MPMusicPlayerController applicationMusicPlayer] setVolume: volume - point.y / 1000.0];
            }
        }
    }

    
    [self.shengyiMove updateValue];
}

- (void)rightPanAction:(UIPanGestureRecognizer *)pan
{
    CGPoint old = [pan velocityInView:pan.view];
    CGFloat bri = [UIScreen mainScreen].brightness;
    CGPoint point = [pan translationInView:pan.view];
    
    if (ABS(ABS(point.x) - old.x) < 10)
    {
        if (point.y < 0)
        {
            if (bri < 1.0)
            {
                [UIScreen mainScreen].brightness = bri + ABS(point.y) / 1000.0;
            }
        }
        else
        {
            if (bri > 0)
            {
                [UIScreen mainScreen].brightness = bri - point.y / 1000.0;
            }
        }
    }

    [self.liangduMove updateValue];
}

@end
